package com.gxuwz.restaurant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.Cart;
import com.gxuwz.restaurant.entity.Info;
import com.gxuwz.restaurant.entity.Order;
import com.gxuwz.restaurant.mapper.CartMapper;
import com.gxuwz.restaurant.mapper.OrderMapper;
import com.gxuwz.restaurant.utils.MapUtils;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoField;
import java.util.*;

@RestController
@RequestMapping("/Statistics")
public class StatisticsController {
    @Resource
    OrderMapper orderMapper;
    @Resource
    CartMapper cartMapper;


    //获取前n个月方法
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }

    @GetMapping("/selectInfo")   //get用来查询
    public Result<?> findinfo() {


        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();
        wrapper.eq(Order::getPayState, "已支付");
        List<Order> info = orderMapper.selectList(wrapper);

        HashMap<String, Integer> hs = new MapUtils();
        for (Order order : info) {
            hs.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs.keySet()) {
            Info info1 = new Info();
            info1.setKey(key);
            info1.setValue(hs.get(key));
            list.add(info1);
        }
        return Result.success(list);
    }

    @GetMapping("/selectInfo_sixMonth")   //get用来查询
    public Result<?> findinfo_sixMonth() {


        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();
        wrapper.eq(Order::getPayState, "已支付");

        System.out.println("查询六个月但现在");
        Date nowdate = new Date();
        Date sixMonthDate = stepMonth(nowdate, -6);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = sdf.format(nowdate);
        String sixmonth = sdf.format(sixMonthDate);
        wrapper.between(Order::getCreatTime, sixmonth, today);

        List<Order> info = orderMapper.selectList(wrapper);

        HashMap<String, Integer> hs = new MapUtils();
        for (Order order : info) {
            hs.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs.keySet()) {
            Info info1 = new Info();
            info1.setKey(key);
            info1.setValue(hs.get(key));
            list.add(info1);
        }
        return Result.success(list);
    }
    @GetMapping("/selectInfo_nowMonth")   //get用来查询
    public Result<?> findinfo_nowMonth() {


        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();
        wrapper.eq(Order::getPayState, "已支付");

        System.out.println("查询本月");
        Date nowdate = new Date();
        Date lastMonthDate = stepMonth(nowdate, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowMonth = sdf.format(nowdate);

        String lastMonth = sdf.format(lastMonthDate);
        System.out.println(nowMonth+"本月"+lastMonth);
        wrapper.between(Order::getCreatTime,lastMonth,nowMonth);

        List<Order> info = orderMapper.selectList(wrapper);
        System.out.println(info);
        HashMap<String, Integer> hs = new MapUtils();
        for (Order order : info) {
            hs.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs.keySet()) {
            Info info1 = new Info();
            info1.setKey(key);
            info1.setValue(hs.get(key));
            list.add(info1);
        }
        return Result.success(list);
    }
    @GetMapping("/selectInfo_today")   //get用来查询
    public Result<?> findinfo_today() {


        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        Wrappers.lambdaQuery();
        wrapper.eq(Cart::getPayState, "已支付");

        System.out.println("查询今日");
        Date nowdate = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(nowdate);



        wrapper.like(Cart::getCreatTime,today);

        List<Cart> info = cartMapper.selectList(wrapper);
        System.out.println(info);
        HashMap<String, Integer> hs = new MapUtils();
        for (Cart cart : info) {
            hs.put(cart.getGoodsname(), Integer.valueOf(cart.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs.keySet()) {
            Info info1 = new Info();
            info1.setKey(key);
            info1.setValue(hs.get(key));
            list.add(info1);
        }
        return Result.success(list);
    }

    @GetMapping("/selectInfo1")   //get用来查询
    public Result<?> findinfo1(@RequestParam String val1, @RequestParam String val2
    ) {
        System.out.println("执行查询1");
        System.out.println(val1);
        System.out.println(val2);

        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();
        wrapper.eq(Order::getPayState, "已支付");
        wrapper.between(Order::getCreatTime, val1, val2);
        List<Order> info = orderMapper.selectList(wrapper);
        System.out.println(info);

        HashMap<Integer, Integer> hs = new MapUtils();
        for (Order order : info) {
            /*获取月份*/
            int month = order.getCreatTime().get(ChronoField.MONTH_OF_YEAR);
            hs.put(month, Integer.valueOf(order.getPrice()));
        }
        System.out.println(hs);
        List<Info> list = new ArrayList();
        for (Integer key : hs.keySet()) {
            Info info1 = new Info();
            info1.setKey(String.valueOf(key));
            info1.setValue(hs.get(key));
            list.add(info1);
        }
        return Result.success(list);
    }

    @GetMapping("/selectInfo2")   //get用来查询
    public Result<?> findinfo2() {
        System.out.println("执行查询2");
        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();
        wrapper.eq(Order::getPayState, "已支付");
        List<Order> info = orderMapper.selectList(wrapper);
        HashMap<String, Integer> hs1 = new MapUtils();
        for (Order order : info) {
            hs1.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs1.keySet()) {
            QueryWrapper<Order> queryWrapper = new QueryWrapper();
            queryWrapper.eq("goodsname", key);
            int number = orderMapper.selectList(queryWrapper).size();
            Info info1 = new Info();
            info1.setKey(String.valueOf(key));
            info1.setValue(number);
            list.add(info1);
        }
        return Result.success(list);
    }
    @GetMapping("/selectInfo2_myChart_sixMonth")   //get用来查询
    public Result<?> findinfo2_myChart_sixMonth() {
        System.out.println("执行查询近半年");
        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();

        wrapper.eq(Order::getPayState, "已支付");

        Date nowdate = new Date();
        Date lastMonthDate = stepMonth(nowdate, -6);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowMonth = sdf.format(nowdate);

        String lastMonth = sdf.format(lastMonthDate);
        System.out.println(nowMonth+"本月"+lastMonth);
        wrapper.between(Order::getCreatTime,lastMonth,nowMonth);

        List<Order> info = orderMapper.selectList(wrapper);
        HashMap<String, Integer> hs1 = new MapUtils();
        for (Order order : info) {
            hs1.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs1.keySet()) {
            QueryWrapper<Order> queryWrapper = new QueryWrapper();
            queryWrapper.eq("goodsname", key);
            queryWrapper.between("creat_time",lastMonth,nowMonth);
            int number = orderMapper.selectList(queryWrapper).size();
            Info info1 = new Info();
            info1.setKey(String.valueOf(key));
            info1.setValue(number);
            list.add(info1);
        }
        return Result.success(list);
    }
    @GetMapping("/selectInfo2_nowMonth")   //get用来查询
    public Result<?> selectInfo2_nowMonth() {
        System.out.println("执行查询近30天");
        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();

        wrapper.eq(Order::getPayState, "已支付");

        Date nowdate = new Date();
        Date lastMonthDate = stepMonth(nowdate, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowMonth = sdf.format(nowdate);

        String lastMonth = sdf.format(lastMonthDate);
        System.out.println(nowMonth+"本月"+lastMonth);
        wrapper.between(Order::getCreatTime,lastMonth,nowMonth);

        List<Order> info = orderMapper.selectList(wrapper);
        HashMap<String, Integer> hs1 = new MapUtils();
        for (Order order : info) {
            hs1.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs1.keySet()) {
            QueryWrapper<Order> queryWrapper = new QueryWrapper();
            queryWrapper.eq("goodsname", key);
            queryWrapper.between("creat_time",lastMonth,nowMonth);
            int number = orderMapper.selectList(queryWrapper).size();
            Info info1 = new Info();
            info1.setKey(String.valueOf(key));
            info1.setValue(number);
            list.add(info1);
        }
        return Result.success(list);
    }
    @GetMapping("/selectInfo2_today")   //get用来查询
    public Result<?> selectInfo2_today() {
        System.out.println("执行查询今天");
        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        Wrappers.lambdaQuery();

        wrapper.eq(Order::getPayState, "已支付");

        Date nowdate = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(nowdate);


        wrapper.like(Order::getCreatTime,today);

        List<Order> info = orderMapper.selectList(wrapper);
        HashMap<String, Integer> hs1 = new MapUtils();
        for (Order order : info) {
            hs1.put(order.getGoodsname(), Integer.valueOf(order.getPrice()));
        }
        List<Info> list = new ArrayList();
        for (String key : hs1.keySet()) {
            QueryWrapper<Order> queryWrapper = new QueryWrapper();
            queryWrapper.eq("goodsname", key);
            queryWrapper.like("creat_time",today);
            int number = orderMapper.selectList(queryWrapper).size();
            Info info1 = new Info();
            info1.setKey(String.valueOf(key));
            info1.setValue(number);
            list.add(info1);
        }
        return Result.success(list);
    }

}
